//
//  CurrentV.m
//  UHealth
//
//  Created by gjtxz on 15/1/7.
//  Copyright (c) 2015年 gjtx. All rights reserved.
//

#import "CurrentV.h"
#import "MJRefresh.h"


@interface CurrentV ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UILabel            *eatHit;
@property (nonatomic,strong) UIButton           *eatAddBtn;
@property (nonatomic,strong) UITableView        *eatTable;
@property (nonatomic,strong) NSMutableArray     *eatArr;

@property (nonatomic,strong) UIButton           *breakfastBtn;
@property (nonatomic,strong) UIButton           *lunchBtn;
@property (nonatomic,strong) UIButton           *dinnerBtn;

@property (nonatomic,strong) UIDatePicker       *pick;

@end

@implementation CurrentV

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        self.contentSize = CGSizeMake(frame.size.width, kDefaultContentSize);
        [self DrawViewWithFrame:frame];
    }
    return self;
}

-(void)DrawViewWithFrame:(CGRect)frame
{
    self.backgroundColor = kBlueColor;
    //实时血糖
    _bspro = [[wendu_yuan2 alloc]initWithFrame:CGRectMake(100, 10, 120, 120)];
    _bspro.backgroundColor = [UIColor clearColor];
    [self addSubview:_bspro];
    
    //参比
    _canbi = [UIButton buttonWithType:UIButtonTypeSystem];
    _canbi.frame = CGRectMake(120, 125, 80, 20);
    [_canbi.layer setMasksToBounds:YES];
    [_canbi.layer setCornerRadius:5.0]; //设置矩形四个圆角半径
    [_canbi setTitle:[NSString stringWithFormat:kLocal(@"canbi"),0] forState:UIControlStateNormal];
    [_canbi setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _canbi.titleLabel.font = [UIFont systemFontOfSize:10.0];
    _canbi.backgroundColor = kWhiteBlueColor;
    [_canbi setImage:[UIImage imageNamed:@"bloodsugar_white"] forState:UIControlStateNormal];
    _canbi.tag = 100;
    _canbi.enabled = NO;
    [self addSubview:_canbi];
    
    
    _stateLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 120, 20)];
    _stateLabel.font = [UIFont systemFontOfSize:10.0];
    _stateLabel.textAlignment = NSTextAlignmentCenter;
    _stateLabel.textColor = [UIColor whiteColor];
    _stateLabel.text = @"状态";
    [self addSubview:_stateLabel];
    
    
    _lastV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 160, frame.size.width, frame.size.height)];
    _lastV.backgroundColor = kBacColor;
//    _lastV.userInteractionEnabled = NO;
    [self addSubview:_lastV];
    _lastVFrame = _lastV.frame;
    
    //添加手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
    pan.minimumNumberOfTouches = 1;
    pan.maximumNumberOfTouches = 1;
    [_lastV addGestureRecognizer:pan];
    
    //进食
    _breakfastBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, frame.size.width/3, 40)];
    [_breakfastBtn setTitle:kLocal(@"Breakfast") forState:UIControlStateNormal];
    [_breakfastBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _breakfastBtn.titleLabel.font = [UIFont systemFontOfSize:12.0];
    _breakfastBtn.backgroundColor =[UIColor whiteColor];
    [_breakfastBtn setImageEdgeInsets: UIEdgeInsetsMake(0, frame.size.width/3-25, 0,0)];
    [_breakfastBtn setTitleEdgeInsets: UIEdgeInsetsMake(0, 0, 0, 25)];
    [_breakfastBtn setImage:[UIImage imageNamed:@"edit"] forState:UIControlStateNormal];
    [_breakfastBtn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
    _breakfastBtn.tag =103;
    
    _lunchBtn = [[UIButton alloc]initWithFrame:CGRectMake(frame.size.width/3, 0, frame.size.width/3, 40)];
    [_lunchBtn setTitle:kLocal(@"Lunch") forState:UIControlStateNormal];
    [_lunchBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _lunchBtn.titleLabel.font = [UIFont systemFontOfSize:12.0];
    _lunchBtn.backgroundColor =[UIColor whiteColor];
    [_lunchBtn setImageEdgeInsets: UIEdgeInsetsMake(0, frame.size.width/3-25, 0,0)];
    [_lunchBtn setTitleEdgeInsets: UIEdgeInsetsMake(0, 0, 0, 25)];
    [_lunchBtn setImage:[UIImage imageNamed:@"edit"] forState:UIControlStateNormal];
    [_lunchBtn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
    _lunchBtn.tag =104;
    
    _dinnerBtn = [[UIButton alloc]initWithFrame:CGRectMake(frame.size.width/3*2 , 0, frame.size.width/3, 40)];
    [_dinnerBtn setTitle:kLocal(@"Dinner") forState:UIControlStateNormal];
    [_dinnerBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _dinnerBtn.titleLabel.font = [UIFont systemFontOfSize:12.0];
    _dinnerBtn.backgroundColor =[UIColor whiteColor];
    [_dinnerBtn setImageEdgeInsets: UIEdgeInsetsMake(0, frame.size.width/3-25, 0,0)];
    [_dinnerBtn setTitleEdgeInsets: UIEdgeInsetsMake(0, 0, 0, 25)];
    [_dinnerBtn setImage:[UIImage imageNamed:@"edit"] forState:UIControlStateNormal];
    [_dinnerBtn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
    _dinnerBtn.tag =105;
    
    [_lastV addSubview:_breakfastBtn];
    [_lastV addSubview:_lunchBtn];
    [_lastV addSubview:_dinnerBtn];
    
    UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"none"]];
    img.frame = CGRectMake(130, 50, frame.size.width-260, 60);
    [_lastV addSubview:img];
    
    _eatArr = [NSMutableArray arrayWithObjects:@{@"img":@"food_blue",@"time":@"08:00",@"thing":@"豆浆、油条"},@{@"img":@"food_blue",@"time":@"08:00",@"thing":@"豆浆、油条"},@{@"img":@"food_blue",@"time":@"08:00",@"thing":@"豆浆、油条"},@{@"img":@"food_blue",@"time":@"08:00",@"thing":@"豆浆、油条"},@{@"img":@"food_blue",@"time":@"08:00",@"thing":@"豆浆、油条"},@{@"img":@"food_blue",@"time":@"08:00",@"thing":@"豆浆、油条"},@{@"img":@"food_blue",@"time":@"08:00",@"thing":@"豆浆、油条"}, nil];
    
    _eatHit = [[UILabel alloc]init];
    _eatHit.font = [UIFont systemFontOfSize:10.0];
    _eatHit.numberOfLines = 0;
    _eatHit.backgroundColor = kBacColor;
    
    _eatAddBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    _eatAddBtn.tag = 106;
    [_eatAddBtn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
    _eatAddBtn.layer.masksToBounds = YES;
    [_eatAddBtn.layer setCornerRadius:5];
    [_eatAddBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    if(!_eatArr || _eatArr.count <= 0){
        
        _eatHit.frame = CGRectMake(50, 110, frame.size.width-100, 50);
        _eatHit.text = kLocal(@"EatHitNone");
        _eatHit.textAlignment = NSTextAlignmentCenter;
        
        _eatAddBtn.frame =CGRectMake(100, 160, frame.size.width - 200,25 );
        _eatAddBtn.backgroundColor = kBlueColor;
        [_eatAddBtn setTitle:kLocal(@"AddRecord") forState:UIControlStateNormal];
        
    }else{
        _eatHit.frame = CGRectMake(10, 40, frame.size.width-35-10, 30);
        _eatHit.text = kLocal(@"EatHit");
        
        _eatAddBtn.frame =CGRectMake(frame.size.width-35, 45, 25, 25);
        [_eatAddBtn setBackgroundImage:[UIImage imageNamed:@"add_index"] forState:UIControlStateNormal];
        [_eatAddBtn setBackgroundImage:[UIImage imageNamed:@"add_bluetooth"] forState:UIControlStateHighlighted];
    }
    
    
    [_lastV addSubview:_eatHit];
    [_lastV addSubview:_eatAddBtn];
    
    
    _eatTable = [[UITableView alloc]initWithFrame:CGRectMake(10, 70,frame.size.width-20,frame.size.height-70) style:UITableViewStyleGrouped];
    _eatTable.dataSource = self;
    _eatTable.delegate = self;
    _eatTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_lastV addSubview:_eatTable];
    
    
}

#pragma mark - func
- (void)BtnClick:(UIButton*)Btn
{
    kDebug(@"点了%d",Btn.tag);
    switch (Btn.tag) {
        case 101:

            break;
        case 102:
            
            break;
        case 103:
            break;
        case 104:
            break;
        case 105:
            break;
        case 106:
            break;
        default:
            break;
    }
}


//手势
- (void)handlePanGesture:(UIPanGestureRecognizer *)recognizer
{
    
    if (recognizer.state == UIGestureRecognizerStateBegan) {

    }
    
    if (recognizer.state == UIGestureRecognizerStateChanged) {
        CGPoint movement = [recognizer translationInView:self];

        if (movement.y > 0 ) {
            if (_lastV.frame.origin.y < _lastVFrame.origin.y + 40) {
            _lastV.transform = CGAffineTransformMakeTranslation(0, movement.y);
            }
        }else if(movement.y > -160){
            if (_lastV.frame.origin.y > _lastVFrame.origin.y -160) {
                _lastV.transform = CGAffineTransformMakeTranslation(0, movement.y);
            }
        }
    }
    
    if (recognizer.state == UIGestureRecognizerStateEnded) {

        CGPoint velocity = [recognizer velocityInView:self];

        CGRect f = _lastVFrame;
        if (velocity.y >= 40) {
        }else if(velocity.y <= -40){
            f.origin.y = f.origin.y - 160;
        }
        
        [UIView animateWithDuration:.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut
                         animations:^{
                             _lastV.transform = CGAffineTransformIdentity;
                             _lastV.frame = f;
                             
                         } completion:^(BOOL finished) {
                             
                         }];
    }
}


#pragma mark -UItableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return _eatArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID =@"cellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    [cell.layer setMasksToBounds:YES];
    [cell.layer setCornerRadius:5.0]; //设置矩形四个圆角半径
    cell.textLabel.textColor = [UIColor blueColor];
    cell.textLabel.font = [UIFont systemFontOfSize:10.0];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:14.0];
    
    NSDictionary *dict = _eatArr[indexPath.section];
    cell.imageView.image = [UIImage imageNamed:[dict objectForKey:@"img"]];
    cell.textLabel.text = [dict objectForKey:@"time"];
    cell.detailTextLabel.text = [dict objectForKey:@"thing"];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}


@end
